namespace LightBox
{
	using System;
	using System.ComponentModel;
	using System.Drawing;
	using System.IO;
	using System.Globalization;
	using System.Security.Permissions;
	using System.Web;
	using System.Web.UI;
	using System.Web.UI.HtmlControls;
	using System.Web.UI.WebControls;

	using LightBox.Design;
	using LightBox.Installer;
	using LightBox.Properties;

	/// <summary>
	/// Creates the place holder control for the scripts and other component registration.
	/// </summary>
	[ToolboxData("<{0}:scriptmanager runat='server'></{0}:scriptmanager>")]
	[PersistChildren(false), NonVisualControl, Designer(typeof(ScriptManagerDesigner))]
	[AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	[AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	public class ScriptManager : WebControl
	{
		private bool installFiles;

		/// <summary>
		/// 
		/// </summary>
		public ScriptManager() : base() { }

		/// <summary>
		/// 
		/// </summary>
		public bool InstallFiles
		{
			get { return installFiles; }
			set { installFiles = value; }
		}

		/// <summary>
		/// 
		/// </summary>
		protected override void CreateChildControls()
		{
			if (this.InstallFiles)
			{
				LightboxInstall.InstallFiles();
			}

			RegisterCssLinks();
			RegisterScriptIncludes();

			base.CreateChildControls();
		}

		/// <summary>
		/// 
		/// </summary>
		protected virtual void RegisterCssLinks()
		{
			if (Page != null && Page.Header != null)
			{
				HtmlLink cssLink = new HtmlLink();
				cssLink.ID = "lightBoxCss";

				string baseUrl = string.Format(CultureInfo.CurrentCulture, "{0}{1}/css/lightbox.css",
						LightboxHelper.ApplicationPath, Resources.LightboxBaseFolder);

				cssLink.Href = baseUrl;
				cssLink.Attributes.Add("rel", "Stylesheet");
				cssLink.Attributes.Add("type", "text/css");
				cssLink.Attributes.Add("media", "screen");

				Page.Header.Controls.Add(cssLink);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		protected virtual void RegisterScriptIncludes()
		{
			if (Page != null && Page.Header != null)
			{
				string baseUrl = string.Format(CultureInfo.CurrentCulture, "{0}{1}/js/",
					LightboxHelper.ApplicationPath, Resources.LightboxBaseFolder);

				foreach (string value in LightboxInstall.Scripts)
				{
					HtmlGenericControl script = new HtmlGenericControl("script");
					script.Attributes.Add("type", "text/javascript");
					script.Attributes.Add("src", baseUrl + value);

					Page.Header.Controls.Add(script);
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="writer"></param>
		protected override void Render(HtmlTextWriter writer)
		{
			string headerStartComment = "<!-- Begin ScriptManager Section -->\n";
			string headerEndComment = "\n<!-- End ScriptManager Section -->";

			string variableScript = @"
				<script type='text/javascript'>
				<!--
					var fileLoadingImage = '" + ResolveUrl("~") + @"lightboxNet/images/loading.gif'; 
					var fileBottomNavCloseImage = '" + ResolveUrl("~") + @"lightboxNet/images/closelabel.gif';
				// -->
				</script>";

			writer.Write(headerStartComment);
			writer.Write(variableScript);
			writer.Write(headerEndComment);
		}
	}
}